MapTree = {};

MapTree.deleteUrl = "";


MapTree.onClick = function (node) {
	var vehicleId = node.id;
	if (node.leaf) {
		var lat = node.attributes.lat;
		var lng = node.attributes.lng;
		MyMap.getMapHandler().setCenter(lat, lng);
	}
}

MapTree.refresh = function () {
	this.treeManager.tree('reload');
}

//创建地图区域树
MapTree.createTree = function () {
	var me = this;
	this.treeManager = $("#mapTree");
	$("#btnReloadMapTree").click(function () {
		me.refresh();
	});

	$("#btnDeleteArea").click(function () {
		var idArray = [];
		var nodes = me.treeManager.tree('getChecked');
		var s = '';
		for (var i = 0; i < nodes.length; i++) {
			var node = nodes[i];
			var id = node.id;
			if (node.leaf) {
				//var id = strId.substring(1);
				idArray.push(id);
				// s += strId.substring(1)+",";
			}
		}
		if (idArray.length > 0) {
			$.iMessager.confirm('确认', '你确定要删除区域吗?', function (r) {
				if (r) {
					me.deleteAreas(idArray);
				}
			});
		} else {
			$.iMessager.alert('提示', '请选择要删除的区域.', 'messager-warning');
		}

	});

	$("#btnEditArea").click(function () {
		var node = me.treeManager.tree('getSelected');
		me.editArea(node);
	});

	var treeUrl = path + "/map/tree";
	this.treeManager.tree({
		checkbox: true, lines: true, url: treeUrl,
		onCheck: function (node, checked) {
			//me.onCheck(node,checked);
		},
		onClick: function (node, checked) {
			me.onClick(node, checked);
		},
		onContextMenu: function (e, node) {
			e.preventDefault();
			$(this).tree('select', node.target);

			//弹出右键修改菜单
			if (node.id.indexOf("v") < 0 && node.id.indexOf("dep") < 0 && (/^[0-9]*$/.test(node.id))) {

				// 显示快捷菜单
				$('#mm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});

			}

		}
	});

}

//修改围栏
MapTree.editArea = function (node) {
	var me = this;
	var id = node.id;
	var type = node.pid;
	if (type == 'rect' || type == 'circle' || type == 'polygon') {
		var url = path + "/area/viewArea?id=" + id;
		parent.openRouteWindow(url, 600, 250, "区域设置");
	}
	if (type == 'marker') {
		var url = path + "/area/viewMarker?id=" + id;
		parent.openRouteWindow(url, 350, 210, "地标设置");
	}
	/*    if (type == 'keyPoint') {
	 var url = path + "/area/viewKeyPoint?EntityId=" + id;
	 parent.openRouteWindow(url, 760, 370, "关键点区域设置");
	 }
	 if (type == 'marker') {
	 var url = path + "/Area.mvc/viewMarker?EntityId=" + id;
	 parent.openRouteWindow(url, 660, 390, "地标设置");
	 }
	 if (type == 'route') {
	 var url = path + "/Area.mvc/getPoline";
	 var params = { EntityId: id };
	 $.ajax({
	 type: "POST",
	 url: url,
	 data: params,
	 error: function () {
	 //alert("网络连接错误，无法读取数据!");
	 //Utility.done();
	 },
	 success: function (result) {
	 var points = result.data.Points;
	 var url = path + "/Area.mvc/viewRoute?EntityID=" + id + "&strRoutePoints=" + points;
	 //打开路线编辑窗口
	 parent.openRouteWindow(url, 820, 590, "路线编辑");
	 }
	 });

	 }  */
}


/**
 * 删除围栏
 * nodeIds 围栏ID的数组[1,45]的形式
 */
MapTree.deleteAreas = function (nodeIds) {
	var selectIds = ""; //要删除的id,以逗号隔开组成字符串，发送给后台
	$.each(nodeIds, function (m, nodeId) {
		selectIds += nodeId + ",";
	});
	if (selectIds != "") {
		selectIds = selectIds.substring(0, selectIds.length - 1);
	}
	var url = path + "/area/deleteAreas";
	var params = {enclosureIds: selectIds};
	var me = this;
	$.ajax({
			type: "POST",
			url: url,
			data: params,
			error: function () {
				//alert("网络连接错误，无法读取数据!");
				//Utility.done();
			},
			success: function (result) {
				if (result.success == false) {
					$.iMessager.alert("提示", "删除失败:" + result.msg, 'messager-error');

				} else {
					//后台确认删除数据库记录后，删除地图上的图元
					$.each(nodeIds, function (m, nodeId) {
						MyMap.getMapHandler().removeMapArea(nodeId);
						var ligjTab = $('#index_tabs').tabs('getTab', '历史轨迹');
						if (ligjTab && ligjTab.find('iframe').length > 0) {
							var iframeWindow1 = ligjTab.find('iframe')[0];
							if (iframeWindow1 && iframeWindow1.contentWindow.removeMapArea) {
								iframeWindow1.contentWindow.removeMapArea(nodeId);
							}
						}
					});

					me.refresh(); //刷新树

					//me.deleteAreas(idArray);//删除地图的围栏图元
					$.iMessager.alert('提示', '删除成功.', 'messager-info');
				}
			}
		}
	);

}


MapTree.checkAllChildNode = function (node, checked) {
	// node.expand();
	node.checked = checked;
	if (node.data.leaf)
		return;
	node.eachChild(function (child) {
		//child.set('checked', checked);
		child.checked = checked;
		//child.fireEvent('checkchange', child, checked);
		if (child.data.leaf == false)
			checkAllChildNode(child, checked);
	});
}
